<?php

define("WWW", "/cosde/");
define("ROOT", $_SERVER["DOCUMENT_ROOT"] . substr(WWW, 1));
define("IMG_PATH", WWW . "includes/img/");

function fecha($date, $hora = false) {
    if (empty($date) || strstr($date, "0000-00-00")) {
        return "No disponible";
    }
    $string = "d/m/Y";
    if ($hora) {
        $string = "d/m/Y H:i";
    }
    return date($string, strtotime($date));
}

function fechaSQL($fecha) {
    if (empty($fecha)) {
        return "";
    }
    $array = explode("/", $fecha);
    if (is_array($array) && count($array) == 3) {
        return $array[2] . "-" . $array[1] . "-" . $array[0];
    }
    return "";
}

function crearSelect($name, $value, $array, $readonly = false) {
    if ($readonly) {
        return $array[$value];
    }
    $html = '<select name="' . $name . '">';
    $html.= '<option value=""></option>';
    foreach ($array as $key => $val) {
        $html.= '<option value="' . $key . '" ' . ($value == $key ? 'selected="selected"' : '') . '>' . $val . '</option>';
    }
    $html.= '</select>';
    return $html;
}

function crearInput($name, $value, $class, $readonly = false) {
    if ($readonly) {
        return $value;
    }
    return '<input type="text" class="' . $class . '" name="' . $name . '" value="' . $value . '" />';
}

function crearInputRango($name, $value, $class, $readonly = false) {
    $value1 = "";
    $value2 = "";

    $array = explode("|", $value);
    if (is_array($array) && count($array) == 2) {
        $value1 = $array[0];
        $value2 = $array[1];
    }

    if ($readonly) {
        $html2 = '';
        if (!empty($value1)) {
            $html2.= ' Desde: ' . $value1;
        }
        if (!empty($value2)) {
            $html2.= ' Hasta: ' . $value2;
        }
        return trim($html2);
    }

    $html = 'Desde: ';
    $html.= '<input type="text" class="' . $class . '" name="' . $name . '_desde" value="' . $value1 . '" /> ';
    $html.= 'Hasta: ';
    $html.= '<input type="text" class="' . $class . '" name="' . $name . '_hasta" value="' . $value2 . '" />';
    return $html;
}

function arrayFiltro($array_data) {
    $array = array();
    foreach ($array_data as $clave => $valor) {
        if (!empty($valor) && substr($clave, 0, 1) != "_") {
            $clave = str_replace("-", ".", $clave);
            if (strstr($clave, "fecha") || strstr($clave, "date")) {
                if (strstr($clave, "_desde")) {
                    $clave = str_replace("_desde", "", $clave);
                    $clave.= ' >=';
                } elseif (strstr($clave, "_hasta")) {
                    $clave = str_replace("_hasta", "", $clave);
                    $clave.= ' <=';
                } else {
                    $clave.= ' LIKE';
                }
                $valor = fechaSQL($valor);
            } else {
                $clave.= ' LIKE';
            }
            $valor = (campoLike($clave) ? '%' . $valor . '%' : $valor);
            $array[$clave] = $valor;
        }
    }
    return $array;
}

function campoLike($campo) {
    $arrayNotLike = array("id", "numero", "number", "fecha", "date", "estado", "status");
    foreach ($arrayNotLike as $str) {
        if (strstr($campo, $str)) {
            return false;
        }
    }
    return true;
}

function getCodigo($id) {
    return "#" . sprintf("%08d", $id);
}

function viewField($label, $value) {
    echo '<div class="field">';
    echo '<label for="' . $label . '">' . $label . '</label>';
    echo '<span class="field">' . $value . '</span>';
    echo '</div>';
}

function createPostData() {
    $array_url = explode("/", $_SERVER['QUERY_STRING']);
    for ($i = 3; $i < count($array_url); $i++) {
        $a = explode(":", $array_url[$i]);
        if (isset($a[0]) && isset($a[1])) {
            $_POST['_' . $a[0]] = $a[1];
        }
    }
    return $_POST;
}

function generateUrl($action) {
    $array = array();
    $array['action'] = $action;
    if (isset($_POST['_sort'])) {
        $array['sort'] = $_POST['_sort'];
    }
    if (isset($_POST['_direction'])) {
        $array['direction'] = $_POST['_direction'];
    }
    if (isset($_POST['_page'])) {
        $array['page'] = $_POST['_page'];
    }
    return $array;
}

function mesEspaniol($mes) {
    switch ($mes) {
        case "January": return "Enero";
        case "February": return "Febrero";
        case "March": return "Marzo";
        case "April": return "Abril";
        case "May": return "Mayo";
        case "June": return "Junio";
        case "July": return "Julio";
        case "August": return "Agosto";
        case "September": return "Septiembre";
        case "October": return "Octubre";
        case "November": return "Noviembre";
        case "December": return "Diciembre";
    }
}

function diaEspaniol($mes) {
    switch ($mes) {
        case "Monday": return "Lunes";
        case "Tuesday": return "Martes";
        case "Wednesday": return "Miércoles";
        case "Thursday": return "Jueves";
        case "Friday": return "Viernes";
        case "Saturday": return "Sábado";
        case "Sunday": return "Domingo";
    }
}